package com.hall.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public  class TimeThisWeekend {
    public static String getWeekByDate(Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);

        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        int day = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return sdf.format(cal.getTime());
    }
    //当前日期后七天
    public static String getNextWeek(Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
        Calendar  calendar = new GregorianCalendar();
        calendar.setTime(time);
        calendar.add(calendar.DATE,7); //把日期往后增加一天,整数  往后推,负数往前移动
        time=calendar.getTime(); //这个时间就是日期往后推一天的结果
        return sdf.format(time);
    }
    public static Date ChangeTime(String time) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
       return date;
    }
    //当前日期后五天
    public static String getNextFiveWeek(Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
        Calendar  calendar = new GregorianCalendar();
        calendar.setTime(time);
        calendar.add(calendar.DATE,4); //把日期往后增加一天,整数  往后推,负数往前移动
        time=calendar.getTime(); //这个时间就是日期往后推一天的结果
        return sdf.format(time);
    }
    //当前日期后六天
    public static String getNextSevenWeek(Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
        Calendar  calendar = new GregorianCalendar();
        calendar.setTime(time);
        calendar.add(calendar.DATE,6); //把日期往后增加一天,整数  往后推,负数往前移动
        time=calendar.getTime(); //这个时间就是日期往后推一天的结果
        return sdf.format(time);
    }
    /**
     * 获取本周的日期
     * @author ywl
     */
    public static List<String> getWeekDay() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> str = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DAY_OF_WEEK, -1);
        }
        Date[] dates = new Date[7];
        for (int i = 0; i < 7; i++) {
            dates[i] = calendar.getTime();
            calendar.add(Calendar.DATE, 1);
        }
        for (Date date : dates) {
            str.add(sdf.format(date));
        }
        return str;
    }

    /**
     * 根据当前周日的日期获取下周的日期
     * @author ywl
     * @param day
     * @return
     */
    public static List<String> getNextWeekDay(String day) {
        List<String> days = new ArrayList<>();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -1);
            cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            cal.add(Calendar.WEEK_OF_YEAR, 1);


            Calendar calendar = Calendar.getInstance();
            Date time = sdf.parse(day);
            calendar.setTime(time);
            //根据日期获取下周日期可修改此行代码
            //这里使用的是当前周日的日期
            calendar.add(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            if (1 == calendar.get(Calendar.DAY_OF_WEEK)) {
                calendar.add(Calendar.DAY_OF_MONTH, -1);
            }
            calendar.setFirstDayOfWeek(Calendar.MONDAY);
            calendar.add(
                    Calendar.DATE,
                    calendar.getFirstDayOfWeek()
                            - calendar.get(Calendar.DAY_OF_WEEK));


            Date[] dates = new Date[7];
            for (int i = 0; i < 7; i++) {
                dates[i] = calendar.getTime();
                calendar.add(Calendar.DATE, 1);
            }
            for (Date date : dates) {
                days.add(sdf.format(date));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return days;
    }
    /***
     获取上周一*/
    public static String getLastWeekMonday(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(date));
        cal.add(Calendar.DATE, -7);
        return sdf.format(cal.getTime());
    }

    public static Date getThisWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }
    //下周一
    public static String getNextWeekMonday(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(date));
        cal.add(Calendar.DATE, 7);
        return  sdf.format(cal.getTime());
    }


}
